﻿using Ewell.BizComponent.UI.Core.VievModel;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay.Dto.Athena;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.Interface.Common.Utils;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Ewell.BizComponent.UI.Main.ViewModel
{
    public class LoginVm:ViewModelBase
    {
        public IDialogCoordinator dialogCoordinator { get; set; } = DialogCoordinator.Instance;
        private string _code;
        public string code
        {
            get => _code;
            set
            {
                if (_code != value)
                {
                    _code = value;
                    OnPropertyChanged(nameof(code));
                }
            }
        }

        private string _passWord;
        public string passWord
        {
            get => _passWord;
            set
            {
                if (_passWord != value)
                {
                    _passWord = value;
                    OnPropertyChanged(nameof(passWord));
                }
            }
        }
        /// <summary>
        /// 院区
        /// </summary>
        private List<HospitalInfoVm> _hospitalInfoList;
        public List<HospitalInfoVm> hospitalInfoList
        {
            get => _hospitalInfoList;
            set
            {
                if (_hospitalInfoList != value)
                {
                    _hospitalInfoList = value;
                    OnPropertyChanged(nameof(hospitalInfoList));
                }
            }
        }
        /// <summary>
        /// 已选择的院区
        /// </summary>
        private HospitalInfoVm _selectedHospitalInfo;
        public HospitalInfoVm selectedHospitalInfo
        {
            get => _selectedHospitalInfo;
            set
            {
                if (_selectedHospitalInfo != value)
                {
                    _selectedHospitalInfo = value;
                    OnPropertyChanged(nameof(selectedHospitalInfo));
                }
            }
        }

        public User user { get; set; }
        public string menuId { get; set; } = "1444450153871544320";

        public Window mainWindow { get; set; }
        /// <summary>
        /// 查询院区
        /// </summary>
        public ICommand QueryHospital => new RelayCommand(async (object param) =>
        {
            if (string.IsNullOrWhiteSpace(code)) return;

            GetHospitalInfoRequest request = new GetHospitalInfoRequest()
            {
                staffCode= code,
            };
            UserDubboService userDubboService = new UserDubboService();
            var gateWayResult = userDubboService.getHospitalInfo.Post<CloudUserResp>(request);
            if (!gateWayResult.success)
            {
                await dialogCoordinator.ShowMessageAsync(this, "警告!", "获取院区信息出错!");
                return;
            }
            if (gateWayResult.@object.hospitalInfos == null)
                return;
            var hospitalList = new List<HospitalInfoVm>();
            foreach (var item in gateWayResult.@object.hospitalInfos)
            {
                hospitalList.Add(new HospitalInfoVm()
                {
                    zone= item.zone,
                    zoneName= item.zoneName,
                });
            }
            this.hospitalInfoList = hospitalList;
            selectedHospitalInfo = this.hospitalInfoList?.FirstOrDefault();
        });
        /// <summary>
        /// 登录
        /// </summary>
        public ICommand Login => new RelayCommand(async (object param) =>
        {
            if(selectedHospitalInfo==null)
            {
                await dialogCoordinator.ShowMessageAsync(this, "错误!", "请选择院区!");
                return;
            }
            LoginRequest loginRequest = new LoginRequest()
            {
                account = code,
                password = EncryptHelper.SHA256EncryptString(passWord),
                zone = selectedHospitalInfo.zone,
            };
            UserDubboService userDubboService = new UserDubboService();
            var gateWayResult = userDubboService.login.Post<User>(loginRequest);
            if(!gateWayResult.success)
            {
                await dialogCoordinator.ShowMessageAsync(this, "错误!", "登录失败!"+gateWayResult.message);
                return;
            }
            user = gateWayResult.@object;
            user.bizSourceCode = "14";
            LoginInfo.menuId = menuId;

            this.mainWindow?.Close();
        });
    }
}
